$(function (){
    let app = new Vue({
        el: "#app",
        data: {
            sysTitle: "固定资源管理系统",
            userID: "", //用户ID
            userName: "", //用户姓名
            userImageSrc: "", //用户头像
            userClass: "",  //用户类型
            userDepartment: "", //用户类型
            userType: "",
            backImageSrc: "images/common/back.jpg",
            subList1: [
                {href:"properties.html", li: " 资源管理"},
                {href:"departments.html", li: " 部门管理"},
                {href:"employees.html", li: " 员工管理"}
            ],
            subList2: [
                {href:"infos.html", li: " 个人信息"},
                {href:"passwords.html", li: " 密码修改"},
                {href:"util/login.html", li: " 注销"}
            ],
            departments: [{name: "资源部1", value: "1"},{name: "资源部2", value: "2"},{name: "资源部3", value: "3"},{name: "资源部4", value: "4"}],
            propertyImageSrc: "",
            imageShow: false,
            dataList: [],//当前页要展示的列表数据
            dialogFormVisible: false,//添加表单是否可见
            dialogFormVisible4Edit:false,//编辑表单是否可见
            formData: {},//表单数据
            formDataAdd: {}, //添加表单数据
            categories: [],
            employees: [],
            uploadImageSrcAdd: "",
            uploadImageSrc: "",
            rules: {//校验规则
                info: [{ required: true, message: '资源规格为必填项', trigger: 'blur' }],
                pid: [{ required: true, message: '资源类型为必选项', trigger: 'blur' }],
                id: [{ required: true, message: '资源编号为必填项', trigger: 'blur' }],
                eid: [{ required: true, message: '资源类型为非必选项', trigger: 'blur' }],
                name: [{ required: true, message: '资源名称为必填项', trigger: 'blur' }],
                image: [{ required: true, message: '资源图片为必填项', trigger: 'blur' }],
                time: [{ required: true, message: '购入时间为必填项', trigger: 'blur' }],
                price: [{ required: true, message: '消费金额为必填项', trigger: 'blur' }],
                save: [{ required: true, message: '保存位置为必填项', trigger: 'blur' }],
            },
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize:6,//每页显示的记录数
                total:0,//总记录数
                id: "",
                name: "",
                eName: "",
                selectCategory: "",
                save: "",
            }
        },
        created(){
            this.getIdAndName();
        },
        methods: {
            getIdAndName() {
                if(sessionStorage.getItem("userID") === null)
                {
                    location.href = "util/login.html";
                }else {
                    this.userID = sessionStorage.getItem("userID").toString();
                    this.userName = sessionStorage.getItem("userName").toString();
                    this.userImageSrc = "../" + sessionStorage.getItem("userImageSrc").toString();
                    this.userClass = sessionStorage.getItem("userClass").toString();
                    this.userDepartment = sessionStorage.getItem("userDepartment").toString();

                    //获取资源类型数据源
                    this.getDataSource();

                    //显示数据
                    this.getAll();
                }
            },

            //获取资源类型数据源
            getDataSource() {
                //获取资源类型数据源
                axios.get("http://localhost:16/categories").then((res)=>{
                   this.categories = res.data.data;
                });

                //获取资源保管人数据源
                axios.get("http://localhost:16/employees").then((res)=>{
                    this.employees = res.data.data;
                });
            },

            //分页查询
            getAll() {
                 //组织参数，拼接url请求地址
                 // console.log(this.pagination.type);
                 param = "?eid="+this.pagination.eName;
                 param +="&name="+this.pagination.name;
                 param +="&id="+this.pagination.id;
                 param +="&save="+this.pagination.save;
                 param +="&pid="+this.pagination.selectCategory;
                 //发送异步请求
                 axios.get("http://localhost:16/properties/"+this.pagination.currentPage+"/"+this.pagination.pageSize+param).then((res)=>{
                     this.pagination.pageSize = res.data.data.size;
                     this.pagination.currentPage = res.data.data.current;
                     this.pagination.total = res.data.data.total;
                     this.dataList = res.data.data.records;
                     for(var i = 0 ; i<this.dataList.length; i++)
                     {
                         this.dataList[i].time = this.dataList[i].time.toString().slice(0, 10);
                         this.dataList[i].image = "../images/upload/" + this.dataList[i].image;
                     }
                 });
            },

            //切换页码
            handleCurrentChange(currentPage) {
                //修改页码值为当前选中的页码值
                this.pagination.currentPage = currentPage;
                //执行查询
                this.getAll();
            },

            //弹出添加窗口
            handleCreate() {
                this.dialogFormVisible = true;
                this.resetForm();
            },

            //重置表单
            resetForm() {
                this.formData = {};
                this.getDataSource();
                this.imageShow = false;
            },

            //添加
            handleAdd () {
                 axios.get("http://localhost:16/properties/"+this.formDataAdd.id).then((res)=>{
                     if(res.data.flag){
                         this.$message.error("系统中已存在该资源编号！");
                     }else {
                         axios.post("http://localhost:16/properties", this.formDataAdd).then((res)=>{
                             if(res.data.flag){
                                 //关闭弹窗
                                 this.dialogFormVisible = false;
                                 this.$message.success(res.data.msg);
                             }else
                                 this.$message.error(res.data.msg);
                         }).finally(()=>{
                             //重新加载数据
                             this.getAll();
                         });
                     }
                 });
            },

            getFileImageAdd() {
                if(this.uploadImageSrcAdd !== "")
                {
                    let fileNext = this.uploadImageSrcAdd.split(".");
                    if(fileNext[fileNext.length-1].toLowerCase() === "gif" || fileNext[fileNext.length-1].toLowerCase() === "jpg" || fileNext[fileNext.length-1].toLowerCase() === "png")
                    {
                        let arr = this.uploadImageSrcAdd.split("\\");
                        this.formDataAdd.image = arr[arr.length-1];
                        //上传文件

                        //赋值头像
                        this.imageShow = true;
                        this.propertyImageSrc = "../images/upload/" +  this.formDataAdd.image;
                        this.$message.success("上传成功！");
                    }else {
                        this.$message.error("上传头像格式不正确！");
                        this.uploadImageSrcAdd = "";
                        this.imageShow = false;
                        return;
                    }
                }
            },

            getFileImageEdit() {
                if(this.uploadImageSrc !== "")
                {
                    let fileNext = this.uploadImageSrc.split(".");
                    if(fileNext[fileNext.length-1].toLowerCase() === "gif" || fileNext[fileNext.length-1].toLowerCase() === "jpg" || fileNext[fileNext.length-1].toLowerCase() === "png")
                    {
                        let arr = this.uploadImageSrc.split("\\");
                        this.formData.image = arr[arr.length-1];
                        //上传文件

                        //赋值头像
                        this.propertyImageSrc = "../images/upload/" +  this.formData.image;
                        this.$message.success("上传成功！");
                    }else {
                        this.$message.error("上传头像格式不正确！");
                        this.uploadImageSrc = "";
                        return;
                    }
                }
            },

            //取消
            cancel(){
                this.dialogFormVisible = false;
                this.dialogFormVisible4Edit = false;
                this.$message.info("操作已取消");
                this.imageShow = false;
            },

            // 删除
            handleDelete(id, name) {
                   this.$confirm("是否要删除"+name+"用户的信息", "删除提示", {type:"info"}).then(()=>{
                       axios.delete("http://localhost:16/properties/"+id).then((res)=>{
                           if(res.data.flag){
                               this.$message.success(res.data.msg);
                           }else
                               this.$message.error(res.data.msg);
                       }).finally(()=>{
                           //重新加载数据
                           this.getAll();
                       });
                   }).catch(()=>{
                       this.$message.info("取消操作");
                   });

            },

            //弹出编辑窗口
            handleUpdate(id) {
                axios.get("http://localhost:16/properties/"+id).then((res)=>{
                    if(res.data.flag && res.data.data != null)
                    {
                        this.dialogFormVisible4Edit = true;
                        this.formData = res.data.data;
                        this.propertyImageSrc = "../images/upload/" +  this.formData.image;
                        this.formData.time = this.formData.time.toString().slice(0, 10);
                    }else {
                       this.$message.error("数据同步失败，自动刷新");
                    }
               }).finally(()=>{
                   this.getAll();
               });
            },

            //修改
            handleEdit() {
                  axios.put("http://localhost:16/properties", this.formData).then((res)=>{
                      if(res.data.flag)
                      {
                          this.dialogFormVisible4Edit = false;
                          this.$message.success(res.data.msg);
                      }else {
                          this.$message.error(res.data.msg);
                      }
                  }).finally(()=>{
                      this.getAll();
                  });
            },
        }
    });
    var left_nav = $(".left-nav");
    var left_lis = left_nav.children("li");
    var left_sub_nav = left_lis[0].children[1];
    $(left_sub_nav).children("li").eq(0).children("a").addClass("current");
   /* lis.addClass("current");*/
});